package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassNameSetMealServiceImpl
 * @Description TODO
 * @Author钰玟
 * @Date2025/2/14 下午12:21
 * @Version 1.0
 **/
@Service
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    SetMealMapper setMealMapper;
    @Autowired
    SetMealDishMapper setMealDishMapper;

    @Autowired
    DishMapper dishMapper;

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //DTO获取 页数、每页显示条数
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        //获取total、records
        Page<SetmealVO> page = setMealMapper.pageQuery(setmealPageQueryDTO);
        //套餐关联菜品的查询
        //List<SetmealDish>

        for (SetmealVO setmealVO : page) {
            List<SetmealDish> setmealDishes = setMealDishMapper.selectSetmealDishBySetmealId(setmealVO.getId());
            setmealVO.setSetmealDishes(setmealDishes);
        }
        long total = page.getTotal();
        List records = page.getResult();
//        传入PageResult对象，封装数据返回前端
        return  new PageResult(total,records);
    }
    /**
     * 新增套餐
     * @param setmealDTO 套餐对象
     * @return void 新增结果
     */
    @Override
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();

        BeanUtils.copyProperties(setmealDTO, setmeal);
        // 保存套餐基本信息
        setMealMapper.insert(setmeal);
        //增加套餐下的菜品信息
//        SetmealDish setmealDish = new SetmealDish();
//        BeanUtils.copyProperties(setmealDTO, setmealDish);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmeal.getId());
            setMealDishMapper.insert(setmealDish);
        }


    }

    /**
     *  根据id查询套餐详情
     * @param id 套餐id 套餐id
     * @return SetmealDTO 套餐详情信息
     */
    @Override
    public SetmealDTO getById(Long id) {
        //返回setmeal对象
        Setmeal setmeal = setMealMapper.selectById(id);
        //返回setmealDish 对象
        List<SetmealDish> setmealDishes = setMealDishMapper.selectSetmealDishBySetmealId(id);

        //封装为 setmealDTO对象
        SetmealDTO setmealDTO = new SetmealDTO();
        BeanUtils.copyProperties(setmeal, setmealDTO);
        setmealDTO.setSetmealDishes(setmealDishes);
        return setmealDTO;
    }

    /**
     * 更新套餐信息
     * @param setmealDTO 套餐对象
     */
    @Override
    public void updateSetMeal(SetmealDTO setmealDTO) {
        //对于套餐基本信息进行直接拷贝进行修改
        Setmeal setMeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setMeal);
        setMealMapper.update(setMeal);
        //对于套餐菜品信息进行更新
        //--1. 先删除套餐下的菜品信息
        for (SetmealDish setmealDish : setmealDTO.getSetmealDishes()) {

            //删除方法为  ====>deleteBySetmealId1
            setMealDishMapper.deleteBySetmealId1(setmealDTO.getId());
        }
        //--2. 再新增套餐下的菜品信息
        setmealDTO.getSetmealDishes().forEach(setMealDish -> {
            setMealDish.setSetmealId(setmealDTO.getId());
            setMealDishMapper.insert(setMealDish);
        });

    }
    /**
     * 修改套餐状态
     * @param status 套餐状态
     * @param id 套餐id
     * @return 修改结果
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        //如果是起售操作
        if (status == StatusConstant.ENABLE){
            //判断套餐下的菜品是否起售
            //---- 如果都是起售的 套餐才可以起售
            List<SetmealDish> Setmealdishes = setMealDishMapper.selectSetmealDishBySetmealId(id);
            if (Setmealdishes.size() > 0){
                for (SetmealDish setmealdish : Setmealdishes) {
                    Long dishId = setmealdish.getDishId();
                    Dish dish = dishMapper.selectById(dishId);
                    if ( StatusConstant.DISABLE == dish.getStatus()){
                        //---- 不是起售 则无法起售
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                }
                //如果是起售 则可以继续
            }
        }
        Setmeal setmeal = Setmeal.builder().id(id).status(status).build();
        setMealMapper.update(setmeal);
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        if (ids.size() > 0){
            for (Long id : ids) {
                //判断套餐是否起售
                Setmeal setmeal = setMealMapper.selectById(id);
                if (StatusConstant.ENABLE == setmeal.getStatus()){
                    throw new RuntimeException(MessageConstant.SETMEAL_ON_SALE);
                }
            }
            //套餐菜品信息删除
            setMealDishMapper.deleteBySetmealId(ids);
            //套餐信息删除
            setMealMapper.deleteBatch(ids);
        }

    }

    //导入的代码
    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setMealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setMealMapper.getDishItemBySetmealId(id);
    }

}
